from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
import subprocess
from Components.UI.mainwindow import Ui_Form
from Components.Tool2 import FileSearcher
from Components.UI.ae import Ui_AE
from Components.UI.awb import Ui_AWB
from Components.UI.color import Ui_COLOR
import sys

class Stream:
    def __init__(self, text_widget):
        self.text_widget = text_widget
    def write(self, message):
        cursor = self.text_widget.textCursor()
        cursor.movePosition(QTextCursor.MoveOperation.End)
        char_format = QTextCharFormat()
        if '[错误]' in message:
            char_format.setForeground(QColor('red'))
        elif '[成功]' in message:
            char_format.setForeground(QColor('green'))
        elif '[警告]' in message:
            char_format.setForeground(QColor(255,150,0))
        else:
            char_format.setForeground(QColor('black'))
        cursor.insertText(message, char_format)
    def flush(self):
        pass

# class MainWindow(QWidget):
#     def __init__(self):
#         super().__init__()
#         self.ui = Ui_Form()
#         self.ui.setupUi(self)
#         sys.stdout = Stream(self.ui.textBrowser)
#         # 创建 FileSearcher 实例
#         self.fileSearcher = FileSearcher(self.ui.lineEdit, self.ui.textBrowser)
#
#         # 连接按钮和功能
#         self.ui.pushButton.clicked.connect(self.fileSearcher.browseFolder)
#         self.ui.pushButton_2.clicked.connect(lambda: self.fileSearcher.saveFolder(self.ui.lineEdit_2))
#         self.ui.run_btn.clicked.connect(self.fileSearcher.runSearch)
#
#
#         self.subwindow_init()
#
#     def subwindow_init(self):
#         ae_widget = QWidget()
#         self.aeui = Ui_AE()
#         self.aeui.setupUi(ae_widget)
#         self.ui.tab_verticalLayout_1.addWidget(ae_widget)
#
#
#
#         awb_widget = QWidget()
#         self.awbui = Ui_AWB()
#         self.awbui.setupUi(awb_widget)
#         self.ui.tab_verticalLayout_2.addWidget(awb_widget)
#
#
#         color_widget = QWidget()
#         self.colorui = Ui_color()
#         self.colorui.setupUi(color_widget)
#         self.ui.tab_verticalLayout_3.addWidget(color_widget)
class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        sys.stdout = Stream(self.ui.textBrowser)

        self.subwindow_init()

        # 创建 FileSearcher 实例，并传递复选框
        self.fileSearcher = FileSearcher(
            self.ui.lineEdit,
            self.ui.textBrowser,
            self.awbui.checkBox,
            self.aeui.checkBox,
            self.colorui.checkBox_2,
            self.colorui.checkBox,
            self.colorui



        )

        # 连接按钮和功能
        self.ui.pushButton.clicked.connect(self.fileSearcher.browseFolder)
        self.ui.pushButton_2.clicked.connect(lambda: self.fileSearcher.saveFolder(self.ui.lineEdit_2))
        self.ui.run_btn.clicked.connect(self.fileSearcher.runSearch)

    def subwindow_init(self):
        ae_widget = QWidget()
        self.aeui = Ui_AE()
        self.aeui.setupUi(ae_widget)
        self.ui.tab_verticalLayout_1.addWidget(ae_widget)

        awb_widget = QWidget()
        self.awbui = Ui_AWB()
        self.awbui.setupUi(awb_widget)
        self.ui.tab_verticalLayout_2.addWidget(awb_widget)

        color_widget = QWidget()
        self.colorui = Ui_COLOR()
        self.colorui.setupUi(color_widget)
        self.ui.tab_verticalLayout_3.addWidget(color_widget)



